package cn.dataling.ai.common;

import java.time.LocalDateTime;

/**
 * @coder taoym
 * @datetime 2021/10/4 17:47
 * @desc
 */
public class Response<T> {
    /**
     * 状态码
     */
    private final String code;
    /**
     * 状态信息
     */
    private final String msg;
    /**
     * 数据
     */
    private final T data;
    /**
     * 当前时间
     */
    private final LocalDateTime current;
    /**
     * 备注
     */
    private final String desc;

    /**
     * @coder taoym
     * @datetime 2021/10/4 17:54
     * @desc 请求成功
     */
    public static <T> Response<T> success(T data) {
        return newInstant("0000", "请求成功", data, "");
    }

    /**
     * @coder taoym
     * @datetime 2021/10/4 18:07
     * @desc 请求失败
     */
    public static <T> Response<T> fail(ResponseStatusCodeEnum statusCode, String desc) {
        return newInstant(statusCode.getCode(), statusCode.getMsg(), null, desc);
    }

    /**
     * @coder taoym
     * @datetime 2021/10/4 18:07
     * @desc 请求失败
     */
    public static <T> Response<T> fail(String code, String msg, String desc) {
        return newInstant(code, msg, null, desc);
    }

    /**
     * @coder taoym
     * @datetime 2021/10/4 17:53
     * @desc 构造response实例对象
     */
    private static <T> Response<T> newInstant(String code, String msg, T data, String desc) {
        return new Response<>(code, msg, data, LocalDateTime.now(), desc);
    }

    public Response(String code, String msg, T data, LocalDateTime current, String desc) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.current = current;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public LocalDateTime getCurrent() {
        return current;
    }

    public String getDesc() {
        return desc;
    }
}
